package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.common.RspResult;
import com.smfx.yxt.common.enums.OfficialApplyStatus;
import com.smfx.yxt.common.enums.Status;
import com.smfx.yxt.common.enums.WorkApprovalType;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.Grid;
import com.smfx.yxt.model.OfficialApply;
import com.smfx.yxt.model.TeacherInfo;
import com.smfx.yxt.page.OrderBy;
import com.smfx.yxt.page.Page;
import com.smfx.yxt.reqVo.OfficialApplyReqVO;
import com.smfx.yxt.service.IOfficialApplyService;
import com.smfx.yxt.service.IOrganizeGroupService;
import com.smfx.yxt.service.IWorkApprovalConfService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.OfficialApplyView;
import com.smfx.yxt.vo.OfficialApprovalQueryVO;

@RequestMapping(Const.TEACHER_ROLE + MenuConst.OFFICIAL_APPROVAL)
@Controller
public class OfficialApprovalController extends BaseController{

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private IOfficialApplyService officialApplyService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;
	@Autowired
	private IWorkApprovalConfService workApprovalConfService;
	
	@RequestMapping(value={"","/"})
	public String workApproval(Model model) {
		AppUser appUser = getAppUser();
		if(StringUtil.isExistPosition(appUser.getTeacher().getPosition(), "1001,2001")) {
			model.addAttribute("approverType", 1);
			return "officialApproval/approverList";
		}
		return "officialApproval/list";
	}
	
	@RequestMapping("apply")
	public String addApply(Model model) {
		return "officialApproval/apply";
	}
	
	//工作审批提交
	@RequestMapping("apply/submit")
	@ResponseBody Object applySubmit(OfficialApplyReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setSchoolId(appUser.getTeacher().getSchoolId());
			reqVO.setTermId(getCurrentTerm().getId());
			reqVO.setApplyerName(appUser.getTeacher().getName());
			reqVO.setApplyerId(appUser.getTeacher().getId());
			officialApplyService.addApply(reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----applySubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	} 
	/**
	 * 分页查询工作审批
	 * @param vo
	 * @return
	 */
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(OfficialApprovalQueryVO vo) {
		try {
			Page<OfficialApply> page = new Page<>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<OfficialApply> list = officialApplyService.selectListByPage(page);
			grid.setRows(BeanUtils.copyAs(list, OfficialApplyView.class));
			grid.setTotal(page.getTotalRecord());
			return new RspResult(Status.SUCCESS, grid);
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 工作审批详情页
	 * @param applyId
	 * @return
	 */
	@RequestMapping("detail/{applyId}")
	public String detail(Model model, @PathVariable Integer applyId) {
		OfficialApplyView detail = officialApplyService.selectViewById(applyId);
		model.addAttribute("detail", detail);
		if(OfficialApplyStatus.PENDING.getCode().equals(detail.getStatus())) {
			model.addAttribute("isPrincipal", StringUtil.isExistPosition(getAppUser().getTeacher().getPosition(), "2001"));
		}else if (OfficialApplyStatus.OFFICE_AUDITING.getCode().equals(detail.getStatus())) {
			model.addAttribute("isOfficer", workApprovalConfService.
					isWorkApprover(WorkApprovalType.OFFICIAL_APPROVAL.getCode(), 
							getAppUser().getTeacher().getId()));
		}
		return "officialApproval/detail";
	}
	/**
	 * 审批
	 * @param reqVO
	 * @return
	 */
	@RequestMapping("success")
	@ResponseBody Object approvalSuccess(OfficialApplyReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setApproverId(appUser.getTeacher().getId());
			reqVO.setApproverName(appUser.getTeacher().getName());
			reqVO.setStatus(OfficialApplyStatus.AGREE.getCode());
			officialApplyService.updateAudit(reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---approvalSuccess error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	/**
	 * 审批驳回
	 * @param reqVO
	 * @return
	 */
	@RequestMapping("reject")
	@ResponseBody Object reject(OfficialApplyReqVO reqVO) {
		try {
			AppUser appUser = getAppUser();
			reqVO.setApproverId(appUser.getTeacher().getId());
			reqVO.setApproverName(appUser.getTeacher().getName());
			reqVO.setStatus(OfficialApplyStatus.PRINCIPAL_REJECT.getCode());
			officialApplyService.updateAudit(reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---reject error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("revoked")
	@ResponseBody Object revoked(Integer applyId) {
		try {
			AppUser appUser = getAppUser();
			OfficialApplyReqVO reqVO = new OfficialApplyReqVO();
			reqVO.setId(applyId);
			reqVO.setApproverId(appUser.getTeacher().getId());
			reqVO.setApproverName(appUser.getTeacher().getName());
			reqVO.setStatus(OfficialApplyStatus.APPLY_EXPIRE.getCode());
			officialApplyService.updateAudit(reqVO);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("---revoked error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("getExecutor")
	@ResponseBody Object getExecutor() {
		try {
			List<TeacherInfo> list = organizeGroupService.selectOfficialCcer();
			return new RspResult(Status.SUCCESS, list);
		} catch (Exception e) {
			logger.error("---getExecutor error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
